package com.miage.itrading.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import com.miage.itrading.dao.RSSDao;
import com.miage.itrading.entites.RSSItem;

public class RSSWidget extends JInternalFrame {

	public static float X 		= 0;
	public static float Y		= 0.5f;
	public static float WIDTH 	= 0.15f;
	public static float HEIGHT 	= 0.5f;

	private Object[][] rows;

	//	private static FenetrePrincipale parent;	

	private ArrayList<RSSItem> items; 

	private static final long serialVersionUID = 3319394193523337002L;

	public RSSWidget(FenetrePrincipale parent){
		super("RSS - iTrading", true, true, true, true);

		//		this.parent = parent;

		setLocation((int) (parent.getScreenSize().width * X), 
				(int) (parent.getScreenSize().height * Y));

		setSize((int) (parent.getScreenSize().width * WIDTH), 
				(int) (parent.getScreenSize().height * HEIGHT));
		setResizable(true);


		try {
			RSSItem item;
			final SimpleDateFormat formatter = new SimpleDateFormat("dd/MM HH:mm");

			RSSDao dao = new RSSDao();

			ArrayList<RSSItem> lasts = (ArrayList<RSSItem>) (dao.getLastItems(100));

			if (lasts.size() > 0) {

				this.items = lasts;

				JPanel panel = new JPanel();

				rows = new Object[lasts.size()][2];

				for (int i=0 ; i<lasts.size() ; ++i) {
					item = lasts.get(i);

					rows[i][0] = formatter.format(item.getDate());
					rows[i][1] = item.getTitre();
				}


				String[] columnNames	= {"Date", "Titre"};
				DefaultTableModel model = new DefaultTableModel(rows, columnNames);

				final JTable table = new JTable(model){    
				    /**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					//Implement table cell tool tips.
				    public String getToolTipText(MouseEvent e) {
				        String tip = null;
				        java.awt.Point p = e.getPoint();
				        int rowIndex = rowAtPoint(p);
				        int colIndex = columnAtPoint(p);
				        int realColumnIndex = convertColumnIndexToModel(colIndex);

				        if (realColumnIndex == 1) {
				            tip = (String) getValueAt(rowIndex, colIndex);

				        } 
				        else { 
				            tip = super.getToolTipText(e);
				        }
				        return tip;
				    }
				};
				table.setCursor(new Cursor(Cursor.HAND_CURSOR));
				table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
				table.setAutoCreateRowSorter(true);
				table.setModel(new MyTableModel());
				table.addMouseListener(new MouseListener() {
					public void mouseReleased(MouseEvent arg0) { }
					public void mousePressed(MouseEvent arg0) { }
					public void mouseExited(MouseEvent arg0) { }
					public void mouseEntered(MouseEvent arg0) { }

					public void mouseClicked(MouseEvent arg0) {
						int row = table.getSelectedRow();
						int column = table.getSelectedColumn();

						if (row != -1 && column != -1) {
							try {
								Desktop.getDesktop().browse(new URI(items.get(row).getUrl()));
							} catch (IOException e) {
								e.printStackTrace();

							} catch (URISyntaxException e) {
								e.printStackTrace();
							}
						}
					}
				});
	
				JTableHeader header = table.getTableHeader();
				header.setTable(table);
				header.setBackground(new Color(200, 200, 200));

				JScrollPane pane = new JScrollPane(table);

				pane.setPreferredSize(this.getSize());

				panel.setSize(this.getSize());
				Dimension d = this.getSize();
				d.setSize(d.getWidth() - 10 , d.getHeight() - 10);
				pane.setSize(d);
				table.setSize(this.getSize());

				panel.add(pane, BorderLayout.WEST);
				add(panel, BorderLayout.WEST);

				repaint();
				updateUI();			        
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		updateUI();
		repaint();
	}


	class MyTableModel extends AbstractTableModel {
		private static final long	serialVersionUID	= 1L;

		private String[] columnNames	= {"Date", "Titre"};
		private Object[][] data			= rows;


		public int getColumnCount() {
			return columnNames.length;
		}

		public int getRowCount() {
			return data.length;
		}

		public String getColumnName(int col) {
			return columnNames[col];
		}

		public Object getValueAt(int row, int col) {
			return data[row][col];	
		}

		public boolean isCellEditable(int row, int col) {
			return false;
		}

		public void setValueAt(Object value, int row, int col) {
			data[row][col] = value;
		}
	}

}
